package com.example.flowermall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.flowermall.entity.Category;

import com.example.flowermall.entity.Result;
import com.example.flowermall.service.ICategoryService;
import com.example.flowermall.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaoman
 * @since 2023-06-15
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    ICategoryService categoryService;
    @Autowired
    IProductService productService;
    @GetMapping("/list")
    public Result list(@RequestParam(value = "categoryName",required = false)String categoryName,
                       @RequestParam(value = "pageNo",required = false)Long pageNo,
                       @RequestParam(value = "pageSize",required = false)Long pageSize){
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.hasLength(categoryName),Category::getCategoryName,categoryName);
        Page<Category> page=new Page<>(pageNo,pageSize);
        Page<Category> page1 = categoryService.page(page, queryWrapper);
        Map<String,Object> data=new HashMap<>();
        Result result=Result.success();
        data.put("total",page.getTotal());
        data.put("rows",page.getRecords());
        result.put("data",data);
        return result;
    }
    //模糊查询
    @GetMapping("/lists")
    public Result selectCategory(@RequestParam(value = "categoryName",required = false)String categoryName){
        List<Category> category = categoryService.selectCategory(categoryName);
        Result result=Result.success();
        result.put("data",category);
        return result;
    }
    @PostMapping
    public Result insertCategory(@RequestBody Category category){
        categoryService.insertCategory(category);
        return  Result.success("添加成功");
    }
    @GetMapping("/{id}")
    public Result getCategoryById(@PathVariable("id")Integer id){
        Category category=categoryService.getCategoryById(id);
        Result result=Result.success();
        result.put("data",category);
        return result;
    }
    @PutMapping
    public Result updateCategory(@RequestBody Category category){
        categoryService.updateCategory(category);
        return Result.success("修改成功");
    }
    @DeleteMapping("/{id}")
    public Result deleteCategory(@PathVariable("id")Integer id){
        categoryService.deleteCategory(id);
        return Result.success("删除成功");
    }

    @GetMapping("/all")
    public Result getAllCategory(){
        List<Category> list = categoryService.getAllCategory();
        Result result=Result.success();
        result.put("data",list);
        return result;
    }

    @GetMapping("/parentId/{id}")
    public Result selectByParentId(@PathVariable("id")Integer id){
        List<Category> categoryList = categoryService.selectByParentId(id);
        Result result=Result.success();
        result.put("categoryList",categoryList);
        return result;
    }


}
